package org.giovanolli.mumi.export;

import java.awt.Color;
import java.io.IOException;
import java.net.MalformedURLException;

import org.giovanolli.mumi.entities.IdentificableEntity;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;

/**
 * The Class AbstractTemplate.
 */
public abstract class AbstractTemplate<T extends IdentificableEntity> {

	/** The font black times. */
	public static Font FONT_BLACK_TIMES = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL, Color.BLACK);

	public static Font FONT_TABLE_HEADER = new Font(Font.TIMES_ROMAN, 10, Font.BOLD, Color.BLACK);
	public static Font FONT_TABLE_ROW = FONT_BLACK_TIMES;

	/**
	 * Apply content.
	 * 
	 * @param document
	 *            the document
	 * @throws BadElementException
	 *             the bad element exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 * @throws DocumentException
	 *             the document exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	protected abstract void applyContent(Document document, T entity) throws BadElementException, MalformedURLException, DocumentException, IOException;

	/**
	 * Gets the template.
	 * 
	 * @param document
	 *            the document
	 * @return the template
	 * @throws BadElementException
	 *             the bad element exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 * @throws DocumentException
	 *             the document exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public Document applyTemplate(Document document, T entity) throws BadElementException, MalformedURLException, DocumentException, IOException {
		this.formatPage(document);
		document.open();
		this.applyContent(document, entity);
		document.close();
		return document;
	}

	/**
	 * Format page.
	 * 
	 * @param document
	 *            the document
	 */
	private final void formatPage(Document document) {
		document.setPageSize(PageSize.A4);
		document.setMargins(50, 50, 50, 50);
	}

	protected final Chunk generateKeyValueChunk(String key, String value) {
		Chunk rv = new Chunk(key + ": " + value);
		return rv;
	}

	protected Cell generateTableHeaderCell(String header) {
		Cell rv = new Cell();
		rv.setHorizontalAlignment("Center");
		rv.setVerticalAlignment("Center");
		rv.add(new Chunk(header, FONT_TABLE_HEADER));
		return rv;
	}

	protected Cell generateTableRowCell(String content) {
		Cell rv = new Cell();
		rv.setHorizontalAlignment("Center");
		rv.setVerticalAlignment("Center");
		rv.add(new Chunk(content, FONT_TABLE_ROW));
		return rv;
	}

	/**
	 * Gets the formatted for header cell.
	 * 
	 * @return the formatted for header cell
	 */
	public final Cell getFormattedForHeaderCell() {
		Cell rv = new Cell();
		rv.setHorizontalAlignment("Center");
		rv.setVerticalAlignment("Center");
		return rv;
	}
}
